package com.stack.knowyouservice.service.impl;


import com.stack.knowyoubase.anotional.TimeOut;
import com.stack.knowyoubase.constant.GlobalConst;
import com.stack.knowyoubean.bean.Schedule;
import com.stack.knowyoudao.dao.ScheduleDao;
import com.stack.knowyouservice.service.ScheduleService;
import org.springframework.context.annotation.Scope;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import java.util.Collection;
import java.util.List;

/**
 * 课程表服务层
 *
 * @author stack
 */
@Service("scheduleService")
@Scope("singleton")
public class ScheduleServiceImpl implements ScheduleService {

    @Resource
    private ScheduleDao scheduleDao;

    /**
     * 课程信息 从认证角色中获取的账号
     *
     * @param tcid 教师号（可以为空）
     * @return 课程信息
     */
    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public List<Schedule> courseInfo(String tcid) {
        return scheduleDao.findSchedule(tcid);
    }

    /**
     * 插入课程
     *
     * @param schedule       课程实体
     * @param authentication 权限信息
     * @return 添加课程标记
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean courseInsert(Schedule schedule, Authentication authentication) {
        //安全校验，防止插入不是你账号插入的课程,通过服务端认证账户进行提交数据的安全检验
        if (!authentication.getName().equals(String.valueOf(schedule.getTcid()))) {
            return false;
        }
        int i = scheduleDao.insertSchedule(schedule);
        return i == 1;
    }

    /**
     * 课程更新 更新的为课程名
     *
     * @param schedule       课程实体
     * @param authentication 权限信息
     * @return 课程更新标记
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean courseUpdate(Schedule schedule, Authentication authentication) {
        //安全检验，检查是否是此账号拥有的课程,不用提交的tcid判读，通过认证的用户账号
        List<Integer> scheduleId = scheduleDao.findScheduleId(authentication.getName());
        boolean contains = scheduleId.contains(schedule.getSid());
        if (contains) {
            int i = scheduleDao.updateSchedule(schedule);
            return i == 1;
        }
        return false;
    }

    /**
     * 课程删除
     *
     * @param schedule       课程实体
     * @param authentication 权限信息
     * @return 课程删除标记
     */
    @Transactional(readOnly = false, propagation = Propagation.REQUIRED, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean courseDelete(Schedule schedule, Authentication authentication) {
        //安全检验，检查是否是此账号拥有的课程,不用提交的tcid进行判断，通过认证的用户账号
        List<Integer> scheduleId = scheduleDao.findScheduleId(authentication.getName());
        boolean contains = scheduleId.contains(schedule.getSid());
        if (contains) {
            int i = scheduleDao.deleteSchedule(schedule);
            return i == 1;
        }
        return false;
    }

    /**
     * 账号是否拥有该课程可以选择
     *
     * @param sid            课程号
     * @param authentication 认证的信息
     * @return 课程选择标记
     */
    @Transactional(readOnly = true, rollbackFor = Throwable.class)
    @TimeOut
    @Override
    public boolean courseChose(Integer sid, Authentication authentication) {
        //排除学生选课限制
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        for (GrantedAuthority authority : authorities) {
            if (GlobalConst.STUDENT_ROLE.equals(authority.getAuthority())) {
                return true;
            }
        }
        List<Integer> scheduleId = scheduleDao.findScheduleId(authentication.getName());
        return scheduleId.contains(sid);

    }


}
